package com.lt.test.provider.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.lt.test.api.KtProdService;
import com.lt.test.common.exception.CustomException;
import com.lt.test.common.util.StringUtils;
import com.lt.test.provider.dao.*;
import com.lt.test.entity.DTO.PageDTO;
import com.lt.test.provider.mapper.ProdMapper;
import com.lt.test.provider.mapper.ProdTypeMapper;
import com.lt.test.provider.mapper.SupplierProdMapper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(version = "1.0.0")
public class KtProdServiceImpl implements KtProdService {
	 
	 @Autowired
	 ProdTypeMapper prodTypeMapper;
	 
	 @Autowired
	 ProdMapper prodMapper;
	 
	 @Autowired
	 KtShop2ProdDao ktShop2ProdDao;
	 
	 @Autowired
	 KtProdSalePropertyDao ktProdSalePropertyDao;
	 
	 @Autowired
	 SupplierProdMapper supplierProdMapper;
	 
	 @Autowired
	 KtOtherDao ktOtherDao;
	 
	 @Autowired
	 KtProdPriceTypeDao  ktProdPriceTypeDao;
	 
	 @Autowired
	 KtProdPriceDao ktProdPriceDao;
	 
	 @Autowired
	 KtSkuDao ktSkuDao;
	 
	 @Autowired
	 KtProdBrandDao ktPrdoBrandDao;
	 
	 @Autowired
	 KtZoneDao ktZoneDao;
	 
	 @Autowired
	 DProdDao dProdDao;
	 
	 @Autowired
	 KtUpdatePriceDao ktUpdatePriceDao;
	 
	 @Autowired
	 KtCenterTightTobaccoDao ktCenterTightTobaccoDao;
	 @Autowired
	 DProdGroupDao dProdGroupDao;
	 
	 @Override
	public List<Map<String, Object>> getSubProdType(Map parameter) {
		// TODO Auto-generated method stub
		 String parentId = parameter.get("parentId") == null ? "0" : parameter.get("parentId").toString();
		 
		return prodTypeMapper.getSubProdType(parentId);
	}
	 
	@Override
	public PageDTO<Map<String,Object>> getProdTypeListByPage(Integer page, Integer pageSize,String filters) {
		// TODO Auto-generated method stub
		Long beginNum = (long) ((page - 1) * pageSize + 1);
		Long endNum = beginNum + pageSize - 1;

		List<Map<String,Object>> typeList = null;
		int recordCount = 0;
		String typeName = null;
		String parentId = null;
		if(filters != null && !filters.trim().equals("")) {
			JSONObject jsonObject = JSONObject.fromObject(filters);
			JSONArray jsonArray = (JSONArray) jsonObject.get("rules");
			for(int i = 0; i < jsonArray.size(); i++) {
				JSONObject result = (JSONObject) jsonArray.get(i);
				if("typeName".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					typeName =  result.getString("data");
				}
				if("parentId".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					parentId =  result.getString("data");
				}
			}
		}
		
		typeList = prodTypeMapper.getProdTypeListByPage(parentId,typeName,beginNum,endNum);
		recordCount = Integer.valueOf(prodTypeMapper.getProdTypeListSize(parentId,typeName).get("total").toString());
		PageDTO<Map<String,Object>> result = new PageDTO<>(page, pageSize, typeList, recordCount);
		return result;
	}
	@Override
	public PageDTO<Map<String, Object>> getProdInfoListByPage(Integer page, Integer pageSize, String filters) {
		// TODO Auto-generated method stub
		Long beginNum = (long) ((page - 1) * pageSize + 1);
		Long endNum = beginNum + pageSize - 1;

		List<Map<String,Object>> infoList = null;
		int recordCount = 0;
		String productCode = null;
		String productName = null;
		String typeId = null;
		String propertyId = null;
		String state = "1";
		String unit = null;
		String remark = null;
		if(filters != null && !filters.trim().equals("")) {
			JSONObject jsonObject = JSONObject.fromObject(filters);
			JSONArray jsonArray = (JSONArray) jsonObject.get("rules");
			for(int i = 0; i < jsonArray.size(); i++) {
				JSONObject result = (JSONObject) jsonArray.get(i);
				if("productCode".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					productCode =  result.getString("data");
				}
				if("productName".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					productName =  result.getString("data");
				}
				if("typeId".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					typeId =  result.getString("data");
				}
				if("propertyId".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					propertyId =  result.getString("data");
				}
				if("state".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					state =  result.getString("data");
				}
				if("unit".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					unit =  result.getString("data");
				}
				if("remark".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					remark =  result.getString("data");
				}
			}
		}
		Map parameter = new HashMap();
		parameter.put("productCode", productCode);
		parameter.put("productName", productName);
		parameter.put("typeId", typeId);
		parameter.put("propertyId", propertyId);
		parameter.put("state", state);
		parameter.put("beginNum", beginNum);
		parameter.put("endNum", endNum);
		parameter.put("unit", unit);
		parameter.put("remark", remark);
		infoList = prodMapper.getProdInfoListByPage(parameter);
		recordCount = prodMapper.getProdInfoListSize(parameter);
		PageDTO<Map<String,Object>> result = new PageDTO<>(page, pageSize, infoList, recordCount);
		return result;
	}
	@Override
	public PageDTO<Map<String, Object>> getProdPriceListByPage(Integer page, Integer pageSize, String filters) {
		// TODO Auto-generated method stub
		Long beginNum = (long) ((page - 1) * pageSize + 1);
		Long endNum = beginNum + pageSize - 1;

		List<Map<String,Object>> priceList = null;
		int recordCount = 0;
		String productCode = null;
		String productName = null;
		String typeId = null;
		String state = null;
		if(filters != null && !filters.trim().equals("")) {
			JSONObject jsonObject = JSONObject.fromObject(filters);
			JSONArray jsonArray = (JSONArray) jsonObject.get("rules");
			for(int i = 0; i < jsonArray.size(); i++) {
				JSONObject result = (JSONObject) jsonArray.get(i);
				if("productCode".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					productCode =  result.getString("data");
				}
				if("productName".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					productName =  result.getString("data");
				}
				if("typeId".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					typeId =  result.getString("data");
				}
				if("state".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					state =  result.getString("data");
				}
			}
		}
		priceList = prodMapper.getProdPriceListByPage(productCode,productName,typeId,state,beginNum,endNum);
		recordCount = Integer.valueOf(prodMapper.getProdPriceListSize(productCode,productName,typeId,state).get("total").toString());
		PageDTO<Map<String,Object>> result = new PageDTO<>(page, pageSize, priceList, recordCount);
		return result;
	}
	@Override
//	@Transactional(rollbackFor = Exception.class)
	public void saveProdType(Map parameter) {
		// TODO Auto-generated method stub
		String id = parameter.get("id") == null ? null : parameter.get("id").toString();
		String parentId =StringUtils.isNullOrEmpty(parameter.get("parentId")) ? "0": parameter.get("parentId").toString();
		String properties =  parameter.get("properties") == null ? null : parameter.get("properties").toString();
		JSONArray array = new JSONArray();
		if(!StringUtils.isNullOrEmpty(properties)){
			array = JSONArray.fromObject(properties);
		}
		if(!StringUtils.isNullOrEmpty(id)){
			//检查parentid 是否相同
//			Map record = prodTypeMapper.findById(id);
//			if(record.get("parentId").toString().equals(parentId)){
				parameter.put("stateTime", new Date());
				prodTypeMapper.updateKtProdType(parameter);
				
				for (int i = 0; array.size() >0 && i < array.size(); i++) {
					JSONObject obj = array.getJSONObject(i);
					Map propertyMap = (Map) obj.toBean(obj,Map.class);
					if(StringUtils.isNullOrEmpty(propertyMap.get("propertyId"))){
						propertyMap.put("typeId", parameter.get("typeId").toString());
						propertyMap.put("createTime", new Date());
						ktProdSalePropertyDao.create(propertyMap);
					}else{
						propertyMap.put("modifyTime", new Date());
						ktProdSalePropertyDao.update(propertyMap);
					}
				}
		
		}else{
			String typeId = null;
			Map maxData = prodTypeMapper.getMaxData(parentId);
			if(maxData == null){
				if(parentId.equals("0")){
					typeId = "01";
					parameter.put("parentId", "0");
					parameter.put("typeId", typeId);
				}else{
					typeId = parentId+"01";
					parameter.put("parentId", parentId);
					parameter.put("typeId", typeId);
				}
				
			}else{
				String maxTypeId = maxData.get("typeId").toString();
				
				String maxTypeIdEndNum = "0";
				if(parentId.equals("0")){
					parameter.put("parentId", "0");
					maxTypeIdEndNum = maxTypeId.substring(0,maxTypeId.length());
				}else{
					parameter.put("parentId", parentId);
					maxTypeIdEndNum = maxTypeId.substring(parentId.length(),maxTypeId.length());
				}
				
				int typeIdEndNum = Integer.valueOf(maxTypeIdEndNum)+1;
				if (typeIdEndNum<10) {
					if(parentId.equals("0")){
						typeId =  "0"+typeIdEndNum;
					}else{
						typeId =  parentId+"0"+typeIdEndNum;
					}
				
					parameter.put("typeId", typeId);
				}else{
					if(parentId.equals("0")){
						typeId = ""+typeIdEndNum;
					}else{
						typeId = parentId+typeIdEndNum;
					}
					
					parameter.put("typeId", typeId);
				}
			}
			parameter.put("id", StringUtils.getUUID());
			parameter.put("stateTime", new Date());
			parameter.put("levelDeep", parameter.get("typeId").toString().length()/2);
			prodTypeMapper.createKtProdType(parameter);
			
			List<Map> propertiesMap = new ArrayList<Map>();
			if(array.size()>0){
				//删除旧属性,保存新属性
				for (int i = 0; array.size() >0 && i < array.size(); i++) {
					JSONObject obj = array.getJSONObject(i);
					Map propertyMap = (Map) obj.toBean(obj,Map.class);
					propertyMap.put("createTime", new Date());
					propertyMap.put("typeId", typeId);
					propertiesMap.add(propertyMap);
				}
				
				ktProdSalePropertyDao.batch(propertiesMap);
			}
			
			
		}
	}

	
	@Override
//	@Transactional(rollbackFor = Exception.class)
	public void saveProdPrice2Shop(Map parameter) {
		// TODO Auto-generated method stub
		String prodList = parameter.get("prodList").toString();
		String shopList =parameter.get("shopList") == null ? null : parameter.get("shopList").toString();
		String checkedZoneCode = parameter.get("checkedZoneCode") == null ? null : parameter.get("checkedZoneCode").toString();
		String userId = parameter.get("operator").toString();
		String priceType = parameter.get("priceType") == null ? null : parameter.get("priceType").toString();
		Date now = new Date();
		JSONArray prodArray = JSONArray.fromObject(prodList);
		
		JSONArray ShopArray = null;
		if(!StringUtils.isNullOrEmpty(shopList)){
			ShopArray = JSONArray.fromObject(shopList);
		}
		if(!StringUtils.isNullOrEmpty(checkedZoneCode)){
			String[] zoneArray = checkedZoneCode.split(",");
			
			List<Map<String,Object>> shopIdList = ktOtherDao.getKtZoneShopIdList(zoneArray);
			ShopArray = JSONArray.fromObject(shopIdList);
		}
		
		List<Map> data = new ArrayList<Map>();
		String batchNo = StringUtils.getUUID();
		List<LinkedHashMap<String,Object>> shopProdList = new ArrayList<LinkedHashMap<String,Object>>();
		for (int i = 0; i < prodArray.size(); i++) {
			
			for (int j = 0; ShopArray.size() > 0 && j < ShopArray.size(); j++) {
				LinkedHashMap<String,Object> item = new LinkedHashMap<String,Object>();
				JSONObject prodObj = prodArray.getJSONObject(i);
				Map orderItem = (Map) prodObj.toBean(prodObj, Map.class);
				orderItem.put("shopId", ShopArray.getJSONObject(j).getString("shopId"));
				item.put("shopId", ShopArray.getJSONObject(j).getString("shopId"));
				item.put("productCode",prodObj.getString("productCode"));
				orderItem.put("id", StringUtils.getUUID());
				orderItem.put("createTime", now);
				orderItem.put("createUser", userId);
				orderItem.put("batchNo", batchNo);
				orderItem.put("priceTypeId", priceType);
				data.add(orderItem);
				shopProdList.add(item);
			}
		}
		//修改历史数据状态
		if(shopProdList.size() > 0){
			ktUpdatePriceDao.batchUpdateByList(shopProdList);
			ktUpdatePriceDao.batch(data);
			ktOtherDao.batchUpdateKtShop2ProdPrice(batchNo);
		}
		
	}
	@Override
	public PageDTO<Map<String, Object>> getShopListByPage(Integer page, Integer pageSize, String filters) {
		// TODO Auto-generated method stub
		Long beginNum = (long) ((page - 1) * pageSize + 1);
		Long endNum = beginNum + pageSize - 1;

		List<Map<String,Object>> infoList = null;
		int recordCount = 0;
		
		String shopName = null;
		String shopType = null;
		String state = null;
		String orgType = null;
		String shopTypeText = null;
		if(filters != null && !filters.trim().equals("")) {
			JSONObject jsonObject = JSONObject.fromObject(filters);
			JSONArray jsonArray = (JSONArray) jsonObject.get("rules");
			for(int i = 0; i < jsonArray.size(); i++) {
				JSONObject result = (JSONObject) jsonArray.get(i);
				if("shopName".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					shopName =  result.getString("data");
				}
				if("shopType".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					shopType =  result.getString("data");
				}
				if("state".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					state =  result.getString("data");
				}
				if("orgType".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					orgType =  result.getString("data");
				}
				if("shopTypeText".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					shopTypeText =  result.getString("data");
				}
			}
		}
		
		infoList = prodMapper.getShopListByPage(shopName,shopType, shopTypeText,state,orgType,beginNum,endNum);
		recordCount = Integer.valueOf(prodMapper.getShopListSize(shopName,shopType,state,orgType).get("total").toString());
		PageDTO<Map<String,Object>> result = new PageDTO<>(page, pageSize, infoList, recordCount);
		return result;
	}
	@Override
//	@Transactional(rollbackFor = Exception.class)
	public void saveProd(Map parameter)  throws CustomException{
		// TODO Auto-generated method stub
		String id = parameter.get("id") == null ? null : parameter.get("id").toString();
		String productCode = parameter.get("productCode").toString();
		
		Map record = prodMapper.getProdRecord(productCode);
		boolean updatePriceFlag = false;
		if(record == null){
			//如果为空则新增
			parameter.put("stateDate", new Date());
			parameter.put("createTime", new Date());
			parameter.put("productTypeId", StringUtils.isNullOrEmpty(parameter.get("productTypeId")) ? "-1" : parameter.get("productTypeId").toString());
			parameter.put("categoryId", StringUtils.isNullOrEmpty(parameter.get("categoryId")) ? "-1" : parameter.get("categoryId").toString());
			dProdDao.create(parameter);
		}else{
			parameter.put("productId", record.get("productId").toString());
			parameter.put("stateDate", new Date());
			parameter.put("modifyTime", new Date());
			dProdDao.update(parameter);
			//如果商品备注是科泰
			if("科泰".equals(parameter.get("remark"))){
				JSONArray prodList = new JSONArray();
				JSONObject prodObj = new JSONObject();
				prodObj.put("productCode", productCode);
				
				//修改名字
				if(!StringUtils.isNullOrEmpty(parameter.get("productName"))){
					if(!parameter.get("productName").equals(record.get("productName"))){
						ktOtherDao.udpateShop2ProdName(productCode, parameter.get("productName").toString());
					}
				}
				
				//比较零售价
				if(!StringUtils.isNullOrEmpty(parameter.get("rtlPrice"))){
					if(!parameter.get("rtlPrice").equals(record.get("rtlPrice"))){
						prodObj.put("rtlPrice", parameter.get("rtlPrice"));
						updatePriceFlag = true;
					}else{
						prodObj.put("rtlPrice", record.get("rtlPrice"));
					}
				}else{
					prodObj.put("rtlPrice", record.get("rtlPrice"));
				}
				//比较批发/团购价
				if(!StringUtils.isNullOrEmpty(parameter.get("dispatchPrice"))){
					if(!parameter.get("dispatchPrice").equals(record.get("dispatchPrice"))){
						prodObj.put("dispatchPrice", parameter.get("dispatchPrice"));
						updatePriceFlag = true;
					}else{
						prodObj.put("dispatchPrice", record.get("dispatchPrice"));
					}
				}else{
					prodObj.put("dispatchPrice", record.get("dispatchPrice"));
				}
				
				//比较会员价
				if(!StringUtils.isNullOrEmpty(parameter.get("vipPrice"))){
					if(!parameter.get("vipPrice").equals(record.get("vipPrice"))){
						prodObj.put("vipPrice", parameter.get("vipPrice"));
						updatePriceFlag = true;
					}else{
						prodObj.put("vipPrice", record.get("vipPrice"));
					}
				}else{
					prodObj.put("vipPrice", record.get("vipPrice"));
				}
				
				if(updatePriceFlag){
					//查询所有的直营店铺
					List<Map<String,Object>> shopList = ktOtherDao.getKtShopIdList();
					prodList.add(prodObj);
					JSONArray shopIdList = JSONArray.fromObject(shopList);
					Map param = new HashMap();
					param.put("prodList", prodList.toString());
					param.put("shopList", shopIdList);
					param.put("operator", parameter.get("operator") == null ? "" : parameter.get("operator").toString());
					saveProdPrice2Shop(param);
					prodList = null;
					shopList = null;
				}
				
			}
		}
		
		
		
		
//		String productType = parameter.get("productType") == null ? null : parameter.get("productType").toString();
//		//如果是组合商品
//		if("2".equals(productType)){
//			String itemList = parameter.get("itemList") == null ? null : parameter.get("itemList").toString();
//			if(!StringUtils.isNullOrEmpty(itemList)){
//				JSONArray array = JSONArray.fromObject(itemList);
//				List<Map> subProductList = new ArrayList<Map>();
//				for (int i = 0; i < array.size(); i++) {
//					JSONObject obj = array.getJSONObject(i);
//					Map data = (Map) obj.toBean(obj,Map.class);
//					data.put("id", StringUtils.getUUID());
//					data.put("productCode", productCode);
//					data.put("state", "1");
//					data.put("createTime", new Date());
//					subProductList.add(data);
//				}
//				//删除旧数据
//				dProdGroupDao.invalidProduct(productCode);
//				//保存新数据
//				dProdGroupDao.batch(subProductList);
//			}
//		}
//		
//		String properties =  parameter.get("properties") == null ? null : parameter.get("properties").toString();
//		JSONArray array = new JSONArray();
//		if(!StringUtils.isNullOrEmpty(properties)){
//			ktSkuDao.deleteByProductCode(productCode);
//			array = JSONArray.fromObject(properties);
//			List<Map> propertiesList = new ArrayList<Map>();
//			for (int i = 0; i < array.size(); i++) {
//				JSONObject obj = array.getJSONObject(i);
//				Map data = (Map) obj.toBean(obj,Map.class);
//				data.put("id", StringUtils.getUUID());
//				data.put("productCode", productCode);
//				data.put("state", "1");
//				data.put("createTime", new Date());
//				propertiesList.add(data);
//			}
//			ktSkuDao.batch(propertiesList);
//		}
	}
	
	@Override
	public List<Map<String, Object>> getGroupProductItem(Map parameter) {
		// TODO Auto-generated method stub
		String productCode = parameter.get("productCode").toString();
		return dProdGroupDao.getGroupProductItems(productCode);
	}
	
	@Override
	public List<Map<String, Object>> getDict(Map parameter) {
		// TODO Auto-generated method stub
		String dictName = parameter.get("dictName").toString();
		return prodMapper.getDict(dictName);
	}

	@Override
	public List<Map<String, Object>> getProdProperty(Map parameter) {
		// TODO Auto-generated method stub
		String typeId = parameter.get("typeId").toString();
		return prodMapper.getProdProperty(typeId);
	}

	@Override
	public PageDTO<Map<String, Object>> getSupplierProdList(Integer page, Integer pageSize, String filters) {
		// TODO Auto-generated method stub
		Long beginNum = (long) ((page - 1) * pageSize + 1);
		Long endNum = beginNum + pageSize - 1;

		List<Map<String,Object>> typeList = null;
		int recordCount = 0;
		String keyword = null;
		String supplierId = null;
		if(filters != null && !filters.trim().equals("")) {
			JSONObject jsonObject = JSONObject.fromObject(filters);
			JSONArray jsonArray = (JSONArray) jsonObject.get("rules");
			for(int i = 0; i < jsonArray.size(); i++) {
				JSONObject result = (JSONObject) jsonArray.get(i);
				if("keyword".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					keyword =  result.getString("data");
				}
				if("supplierId".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					supplierId =  result.getString("data");
				}
			}
		}
		Map parameter = new HashMap();
		parameter.put("supplierId", supplierId);
		parameter.put("keyword", keyword);
		parameter.put("beginNum", beginNum);
		parameter.put("endNum", endNum);
		typeList = supplierProdMapper.getSupplierProdList(parameter);
		recordCount = supplierProdMapper.getSupplierProdListSize(parameter);
		PageDTO<Map<String,Object>> result = new PageDTO<>(page, pageSize, typeList, recordCount);
		return result;
	}

	@Override
	public void saveSupplierProd(Map parameter) throws CustomException{
		// TODO Auto-generated method stub
		String id = parameter.get("id") == null ? null : parameter.get("id").toString();
		if(StringUtils.isNullOrEmpty(id)){
			//查询是否重复添加
			String supplierId = parameter.get("supplierId").toString();
			String productCode = parameter.get("productCode").toString();
			Map record = supplierProdMapper.getRecordBySupplierIdAndProductCode(parameter);
			if(record != null){
				throw new CustomException("该商品已添加!");
			}
			
			parameter.put("id", StringUtils.getUUID());
			parameter.put("createTime", new Date());
			supplierProdMapper.createKtSupplierProd(parameter);
		}else{
			parameter.put("modifyTime", new Date());
			supplierProdMapper.updateKtSupplierProd(parameter);
		}
	}

	@Override
	public List<Map<String, Object>> getProdPriceType() {
		// TODO Auto-generated method stub
		return ktProdPriceTypeDao.getProdPriceType();
	}

	@Override
	public PageDTO<Map<String, Object>> getProdTypePriceListByPage(Integer page, Integer pageSize, String filters) {
		// TODO Auto-generated method stub
		Long beginNum = (long) ((page - 1) * pageSize + 1);
		Long endNum = beginNum + pageSize - 1;

		List<Map<String,Object>> priceList = null;
		int recordCount = 0;
		String priceType = null;
		String typeId = null;
		String productCode = null;
		String productName = null;
		if(filters != null && !filters.trim().equals("")) {
			JSONObject jsonObject = JSONObject.fromObject(filters);
			JSONArray jsonArray = (JSONArray) jsonObject.get("rules");
			for(int i = 0; i < jsonArray.size(); i++) {
				JSONObject result = (JSONObject) jsonArray.get(i);
				if("priceType".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					priceType =  result.getString("data");
				}
				if("typeId".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					typeId =  result.getString("data");
				}
				if("productCode".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					productCode =  result.getString("data");
				}
				if("productName".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					productName =  result.getString("data");
				}
			}
		}
		priceList = ktProdPriceDao.getProdPriceListByPage(typeId, priceType,productCode,productName, beginNum, endNum);
		recordCount = Integer.valueOf(ktProdPriceDao.getProdPriceListSize(typeId, priceType,productCode,productName).get("total").toString());
		PageDTO<Map<String,Object>> result = new PageDTO<>(page, pageSize, priceList, recordCount);
		return result;
	}
	
	@Override
	public void updateProdDiscountPriceByType(Map parameter) {
		// TODO Auto-generated method stub
		String typeId = parameter.get("typeId") == null ? null : parameter.get("typeId").toString();
		String prodList = parameter.get("prodList") == null ? null : parameter.get("prodList").toString();
		String priceType = parameter.get("priceType") == null ? null : parameter.get("priceType").toString();
		Double discount = Double.valueOf(parameter.get("discount").toString());
		
		if(!StringUtils.isNullOrEmpty(prodList)){
			JSONArray array = JSONArray.fromObject(prodList);
			for (int i = 0; i < array.size(); i++) {
				JSONObject obj = array.getJSONObject(i);
				//查询改商品是否存在，存在修改，不存在新增
				Map record = ktProdPriceDao.getRecord(obj.getString("productCode"));
				if(record == null){
					record = new HashMap();
					record.put("id", StringUtils.getUUID());
					record.put("productCode", obj.getString("productCode"));
					record.put("discount", discount);
					record.put("price", obj.getDouble("rtlPrice"));
					record.put("finalPrice", obj.getDouble("rtlPrice")* discount);
					record.put("state", "1");
					record.put("stateTime", new Date());
					ktProdPriceDao.create(record);
				}else{
					record.put("discount", discount);
					record.put("productCode", obj.getString("productCode"));
					record.put("price", obj.getDouble("rtlPrice"));
					record.put("finalPrice", obj.getDouble("rtlPrice")* discount);
					record.put("state", "1");
					record.put("stateTime", new Date());
					ktProdPriceDao.update(record);
				}
			}
		}
		if(!StringUtils.isNullOrEmpty(typeId)){
//			ktProdPriceDao.updateProdDiscountPriceByType(typeId, priceType, discount);
		}
		
		
	}

	@Override
	public PageDTO<Map<String, Object>> getProdBrandListByPage(Integer page, Integer pageSize, String filters) {
		// TODO Auto-generated method stub
		Long beginNum = (long) ((page - 1) * pageSize + 1);
		Long endNum = beginNum + pageSize - 1;

		List<Map<String,Object>> typeList = null;
		int recordCount = 0;
		String brandName = null;
		String parentId = null;
		String state = null;
		if(filters != null && !filters.trim().equals("")) {
			JSONObject jsonObject = JSONObject.fromObject(filters);
			JSONArray jsonArray = (JSONArray) jsonObject.get("rules");
			for(int i = 0; i < jsonArray.size(); i++) {
				JSONObject result = (JSONObject) jsonArray.get(i);
				if("brandName".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					brandName =  result.getString("data");
				}
				if("parentId".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					parentId =  result.getString("data");
				}
				if("state".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					state =  result.getString("data");
				}
			}
		}
		
		typeList = ktPrdoBrandDao.getProdBrandListByPage(parentId,brandName,state, beginNum, endNum);
		recordCount = Integer.valueOf(ktPrdoBrandDao.getProdBrandListSize(parentId,brandName,state).get("total").toString());
		PageDTO<Map<String,Object>> result = new PageDTO<>(page, pageSize, typeList, recordCount);
		return result;
	}

	
	@Override
	public void saveProdBrand(Map parameter) {
		// TODO Auto-generated method stub
		
		
		String id = parameter.get("id") == null ? null : parameter.get("id").toString();
		String parentId =StringUtils.isNullOrEmpty(parameter.get("parentId")) ? "0": parameter.get("parentId").toString();
		if(!StringUtils.isNullOrEmpty(id)){
			//检查parentid 是否相同
				parameter.put("stateTime", new Date());
				ktPrdoBrandDao.update(parameter);
		}else{
			String typeId = null;
			Map maxData = ktPrdoBrandDao.getMaxData(parentId);
			if(maxData == null){
				if(parentId.equals("0")){
					typeId = "01";
					parameter.put("parentId", "0");
					parameter.put("brandCode", typeId);
				}else{
					typeId = parentId+"01";
					parameter.put("parentId", parentId);
					parameter.put("brandCode", typeId);
				}
				
			}else{
				String maxTypeId = maxData.get("brandCode").toString();
				
				String maxTypeIdEndNum = "0";
				if(parentId.equals("0")){
					parameter.put("parentId", "0");
					maxTypeIdEndNum = maxTypeId.substring(0,maxTypeId.length());
				}else{
					parameter.put("parentId", parentId);
					maxTypeIdEndNum = maxTypeId.substring(parentId.length(),maxTypeId.length());
				}
				
				int typeIdEndNum = Integer.valueOf(maxTypeIdEndNum)+1;
				if (typeIdEndNum<10) {
					if(parentId.equals("0")){
						typeId =  "0"+typeIdEndNum;
					}else{
						typeId =  parentId+"0"+typeIdEndNum;
					}
				
					parameter.put("brandCode", typeId);
				}else{
					if(parentId.equals("0")){
						typeId = ""+typeIdEndNum;
					}else{
						typeId = parentId+typeIdEndNum;
					}
					
					parameter.put("brandCode", typeId);
				}
			}
			parameter.put("id", typeId);
			parameter.put("stateTime", new Date());
			parameter.put("levelDeep", parameter.get("brandCode").toString().length()/2);
			ktPrdoBrandDao.create(parameter);
		}
	}

	@Override
	public PageDTO<Map<String, Object>> getRecommendProdByPage(Integer page, Integer pageSize, String filters) {
		// TODO Auto-generated method stub
		Long beginNum = (long) ((page - 1) * pageSize + 1);
		Long endNum = beginNum + pageSize - 1;

		List<Map<String,Object>> infoList = null;
		int recordCount = 0;
		String shopId = null;
//		String productName = null;
//		String typeId = null;
//		String propertyId = null;
		String type = null;
		if(filters != null && !filters.trim().equals("")) {
			JSONObject jsonObject = JSONObject.fromObject(filters);
			JSONArray jsonArray = (JSONArray) jsonObject.get("rules");
			for(int i = 0; i < jsonArray.size(); i++) {
				JSONObject result = (JSONObject) jsonArray.get(i);
				if("shopId".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					shopId =  result.getString("data");
				}
//				if("productName".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
//					productName =  result.getString("data");
//				}
//				if("typeId".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
//					typeId =  result.getString("data");
//				}
//				if("propertyId".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
//					propertyId =  result.getString("data");
//				}
				if("type".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					type =  result.getString("data");
				}
			}
		}
		if(type != null && type.equals("05")){
			infoList = prodMapper.getRecommendCigaretteList(shopId, beginNum, endNum);
			recordCount = Integer.valueOf(prodMapper.getRecommendCigaretteListSize(shopId).get("total").toString());
		}else {
			infoList = prodMapper.getRecommendProdList(shopId, beginNum, endNum);
			recordCount = Integer.valueOf(prodMapper.getRecommendProdListSize(shopId).get("total").toString());
		}
		PageDTO<Map<String,Object>> result = new PageDTO<>(page, pageSize, infoList, recordCount);
		return result;
	}

	@Override
	public PageDTO<Map<String, Object>> getCenterRecommendProdByPage(Integer page, Integer pageSize, String filters) {
		// TODO Auto-generated method stub
		Long beginNum = (long) ((page - 1) * pageSize + 1);
		Long endNum = beginNum + pageSize - 1;

		List<Map<String,Object>> infoList = null;
		int recordCount = 0;
		String shopId = null;
		String shopName = null;
		String supplierName = null;
		if(filters != null && !filters.trim().equals("")) {
			JSONObject jsonObject = JSONObject.fromObject(filters);
			JSONArray jsonArray = (JSONArray) jsonObject.get("rules");
			for(int i = 0; i < jsonArray.size(); i++) {
				JSONObject result = (JSONObject) jsonArray.get(i);
				if("shopId".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					shopId =  result.getString("data");
				}
				if("shopName".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					shopName =  result.getString("data");
				}
				if("supplierName".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					supplierName =  result.getString("data");
				}
			}
		}
		infoList = prodMapper.getCenterRecommendProdList(shopId, shopName, supplierName, beginNum,endNum);
		recordCount = Integer.valueOf(prodMapper.getCenterRecommendProdListSize(shopId, supplierName).get("total").toString());
		PageDTO<Map<String,Object>> result = new PageDTO<>(page, pageSize, infoList, recordCount);
		return result;
	}

	@Override
	public List<Map<String, Object>> getProdSupplier(Map parameter) {
		// TODO Auto-generated method stub
		String productCode = parameter.get("productCode").toString();
		return ktOtherDao.getProdSupplier(productCode);
	}

	@Override
	public PageDTO<Map<String, Object>> getCenterRecommendTightTobaccoByPage(Integer page, Integer pageSize,
			String filters) {
		// TODO Auto-generated method stub
		Long beginNum = (long) ((page - 1) * pageSize + 1);
		Long endNum = beginNum + pageSize - 1;

		List<Map<String,Object>> infoList = null;
		int recordCount = 0;
		String shopId = null;
		if(filters != null && !filters.trim().equals("")) {
			JSONObject jsonObject = JSONObject.fromObject(filters);
			JSONArray jsonArray = (JSONArray) jsonObject.get("rules");
			for(int i = 0; i < jsonArray.size(); i++) {
				JSONObject result = (JSONObject) jsonArray.get(i);
				if("shopId".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
					shopId =  result.getString("data");
				}
			}
		}
		infoList = prodMapper.getCenterRecommendTightTobaccoList(shopId,beginNum,endNum);
		recordCount = Integer.valueOf(prodMapper.getCenterRecommendTightTobaccoListSize(shopId).get("total").toString());
		PageDTO<Map<String,Object>> result = new PageDTO<>(page, pageSize, infoList, recordCount);
		return result;
	}
	
	@Override
	public void deleteTightTobacco() {
		// TODO Auto-generated method stub
		ktCenterTightTobaccoDao.deleteRecord();
	}

	@Override
	public void saveTightTobacco(Map parameter) {
		// TODO Auto-generated method stub
		String prodList = parameter.get("prodList").toString();
		JSONArray array = JSONArray.fromObject(prodList);
		List<Map> productList = new ArrayList<Map>();
		for (int i = 0; i < array.size(); i++) {
			JSONObject obj = array.getJSONObject(i);
			Map data = (Map) obj.toBean(obj,Map.class);
			productList.add(data);
		}
		ktCenterTightTobaccoDao.batch(productList);
	}

	@Override
	public Map<String, Object> gatStockProd(Map parameter) {
	 	String barCode = parameter.get("barCode").toString();
		String shopId = parameter.get("shopId").toString();
		List<Map<String, Object>> list = ktShop2ProdDao.selectStockProd(barCode, shopId);
		if(list.size() > 0){
			return list.get(0);
		}
		return null;
	}
}
